package com.shuangk.majingze.coolpapa.model;

import android.content.Context;
import android.content.SharedPreferences;


public class UserCache {

    private static final String ACCOUNT_CENTER_USER = "CoolUser";
    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";

    private Context context;

    private UserCache(Context context) {
        this.context = context;
    }

    public static UserCache getInstance(Context context) {
        return new UserCache(context);
    }

    private SharedPreferences getUserPref() {
        return context.getSharedPreferences("user", Context.MODE_PRIVATE);
    }

    public void setUser(User user) {
        getUserPref().edit().putString(ACCOUNT_CENTER_USER, user.toJson().toString()).apply();
    }

    public void clearUser() {
        getUserPref().edit().remove(ACCOUNT_CENTER_USER).apply();
    }

    public User getUser() {
        SharedPreferences sp = getUserPref();
       return new User(sp.getString(USERNAME, ""), sp.getString(PASSWORD, ""));
    }
}
